package com.jiyinit.web.core.mobile.controller;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.base.util.Check;
import com.jiyinit.core.framework.base.util.JsonEntityTransform;
import com.jiyinit.core.framework.base.util.LogUtil;
import com.jiyinit.web.core.business.dto.UserAttendanceRequest;
import com.jiyinit.web.core.business.entity.TabUserAttendanceEntity;
import com.jiyinit.web.core.business.service.UserAttendanceServiceImpl;
import com.jiyinit.web.core.common.logic.ParamCheckLogic;
import com.jiyinit.web.core.common.logic.ValidateResult;
import com.jiyinit.web.core.common.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

 /**
  * 用户考勤相关
  * @author liuzongguang
  * @create 2018/8/29 16:21
  */
@Controller
@RequestMapping("/mobile/attendance")
public class MuserAttendanceController {
    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(MuserAttendanceController.class);

     @Resource(name = "core.paramCheckLogic")
     private ParamCheckLogic paramCheckLogic;

    @Resource(name = "core.userAttendanceImpl")
    private UserAttendanceServiceImpl userAttendanceService;

     /**
      * 打卡考勤
      * @author liuzongguang
      * @create 2018/8/29 16:27
      */
    @ResponseBody
    @RequestMapping(value = "add")
    public ResponseEntity<ResponseDto> add(HttpServletRequest request, @RequestParam String request_json) {
        LogUtil.info(LOGGER, "接口:{}", "/mobile/attendance/add");
        String logJson = request.getParameter("request_json");
        LogUtil.info(LOGGER, "logJson:{}", logJson);
        UserAttendanceRequest userAttendanceRequest = JsonEntityTransform.json2Object(logJson, UserAttendanceRequest.class);
        // 校验参数
        ValidateResult<UserAttendanceRequest> validateResult = paramCheckLogic.checkParamValidate(userAttendanceRequest);
        if (!validateResult.isSuccess()) {
            return new ResponseEntity<>(validateResult.getDto(), HttpStatus.OK);
        }
        if(Check.isNullStr(userAttendanceRequest.getUserId()))
        {
            return new ResponseEntity<>(ResponseDto.responseOK("用户编号不能为空"), HttpStatus.OK);
        }
        if(Check.isNullStr(userAttendanceRequest.getLocation()))
        {
            return new ResponseEntity<>(ResponseDto.responseOK("考勤位置不能为空"), HttpStatus.OK);
        }

        //服务器时间作为考勤时间，防止篡改时间
        userAttendanceRequest.setTimeStamp(DateUtil.getFormatDateToString(new Date(),"yyyy-MM-dd HH:mm:ss"));

        int flag=userAttendanceService.insert(userAttendanceRequest);
        LogUtil.info(LOGGER, "接口:/mobile/attendance/add,返回结果{}", JsonEntityTransform.object2Json(flag));
        if(flag>0)
        {
            return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
        }
        return new ResponseEntity<>(ResponseDto.responseFail("失败"), HttpStatus.OK);
    }

      /**
       * 获取考勤列表
       * @author liuzongguang
       * @create 2018/8/29 17:16
       */
     @ResponseBody
     @RequestMapping(value = "getAttendanceList")
     public ResponseEntity<ResponseDto> getAttendanceList(HttpServletRequest request, @RequestParam String request_json) {
         LogUtil.info(LOGGER, "接口:{}", "/mobile/attendance/getAttendanceList");
         String logJson = request.getParameter("request_json");
         LogUtil.info(LOGGER, "logJson:{}", logJson);
         UserAttendanceRequest userAttendanceRequest = JsonEntityTransform.json2Object(logJson, UserAttendanceRequest.class);
         // 校验参数
         ValidateResult<UserAttendanceRequest> validateResult = paramCheckLogic.checkParamValidate(userAttendanceRequest);
         if (!validateResult.isSuccess()) {
             return new ResponseEntity<>(validateResult.getDto(), HttpStatus.OK);
         }
         if(Check.isNullStr(userAttendanceRequest.getUserId()))
         {
             return new ResponseEntity<>(ResponseDto.responseOK("用户编号不能为空"), HttpStatus.OK);
         }

         PagingResult<TabUserAttendanceEntity> result=userAttendanceService.getAttendanceList(userAttendanceRequest);
         LogUtil.info(LOGGER, "接口:/mobile/attendance/add,返回结果{}", JsonEntityTransform.object2Json(result));
         return new ResponseEntity<>(ResponseDto.responseOK(result), HttpStatus.OK);
     }
}
